package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.dao.IEmailNewsLetterRelaDao;
import com.magicode.cms.dao.INewsEnterpriseRelaDao;
import com.magicode.cms.dao.ISearchConditionDictionaryDao;
import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.EmailNewsLetterRela;
import com.magicode.cms.entiy.base.NewsEnterpriseRela;
import com.magicode.cms.entiy.base.SearchConDicRela;
import com.magicode.cms.entiy.base.SearchConditionDictionary;
import com.magicode.cms.service.IEmailNewsLetterRelaService;
import com.magicode.cms.service.INewsEnterpriseRelaService;
import com.magicode.cms.service.ISearchConditionDictionaryService;

@Service("serviceSearchConditionDictionary")
public class SearchConditionDictionaryServiceImpl extends BaseServiceImpl<SearchConditionDictionary,ISearchConditionDictionaryDao>  implements ISearchConditionDictionaryService{
	@Autowired
	@Qualifier("searchConditionDictionaryDao")
	private ISearchConditionDictionaryDao searchConditionDictionaryDao;
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql =" from " +SearchConditionDictionary.class.getName()+" "+entity_alias_name+" where 1 = 1 ";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
			if(map.containsKey(field_title)){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get(field_title)+"%'";
			}
			if(map.containsKey("valid")){
				hql = hql+" and "+entity_alias_name+".valid='"+map.get("valid")+"'";
			}
		}
		
		return hql;
	}


	
	@Override
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere)+"order by "+entity_alias_name+".title";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public SearchConditionDictionary getSourceByTitle(String name) throws Exception {
		try {
			Map<String,String> map = new HashMap<String,String> ();
			map.put("title", name);
			List<SearchConditionDictionary> sources = this.getDao().queryDataList(this.getHql(map), 0, 0);
			return  sources.size()==1?sources.get(0) : null;
		} catch (Exception e) {
			//throw new Exception("getSourceByTitle error by email=["+name+"]"+e.getMessage());
			throw new Exception("getSourceByTitle error by title=["+name+"]"+e.getMessage());
		}
	}
	@Override
	public void deleteSearchConditionDictionary(String userid) {
		// TODO Auto-generated method stub
		String hql = " from "+SearchConditionDictionary.class.getName()+" t " +
				"  where  t.id = '"+userid+"'" ;
		List<SearchConditionDictionary> vphs = this.searchConditionDictionaryDao.getCommonDaoService().getListByHQL(hql);
		this.searchConditionDictionaryDao.getCommonDaoService().deleteAll(vphs);
	}
	
	@Override
	public void deletesearchConDicRelaList(String userid) {
		// TODO Auto-generated method stub
		String hql = " from "+SearchConDicRela.class.getName()+" t " +
				"  where  t.searchCondition.id = '"+userid+"'" ;
		List<SearchConDicRela> vphs = this.searchConditionDictionaryDao.getCommonDaoService().getListByHQL(hql);
		this.searchConditionDictionaryDao.getCommonDaoService().deleteAll(vphs);
	}
	@Override
	public ISearchConditionDictionaryDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return searchConditionDictionaryDao;
	}
	
	


	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "标题");
		map.put("width", 200);
		beaninfo.add(map);
		map.put(json_key, "code");
		map.put(json_title, "编号");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "valid");
		map.put(json_title, "有效性");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		map.put("width", 50);
		beaninfo.add(map);
		
		return beaninfo;
	}


	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
			
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere,0, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += "  order by "+entity_alias_name+".orderCode asc "; 
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("title", ""+entity_alias_name+".title as title ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("valid", "case when "+entity_alias_name+".valid = 0 then '无效' "
					+ " when "+entity_alias_name+".valid = 1 then '有效' "
					+ " end as valid ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}

//	@Override
//	public List<SearchConDicRela> getSearchConCountryRela(String id) {
//		List<SearchConDicRela> videoList = new ArrayList();
//		SearchConditionDictionary enterprise = null;
//		Map<String, String> map = new HashMap<String, String>();
//		map.put("id", id);
//		String[] fetchJoins = new String[1];
//		fetchJoins[0] = "searchConCountryRelaList";
//		try {
//			enterprise =super.getObject(id,fetchJoins);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		videoList = enterprise.getSearchConCountryRelaList();
//		return videoList;
//	}
//
//	@Override
//	public List<SearchConDicRela> getSearchConcityRela(String id) {
//		List<SearchConDicRela> videoList = new ArrayList();
//		SearchConditionDictionary enterprise = null;
//		Map<String, String> map = new HashMap<String, String>();
//		map.put("id", id);
//		String[] fetchJoins = new String[1];
//		fetchJoins[0] = "searchConCityRelaList";
//		try {
//			enterprise =super.getObject(id,fetchJoins);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		videoList = enterprise.getSearchConCityRelaList();
//		return videoList;
//	}
	public ISearchConditionDictionaryDao getSearchConditionDictionaryDao() {
		return searchConditionDictionaryDao;
	}



	public void setSearchConditionDictionaryDao(
			ISearchConditionDictionaryDao searchConditionDictionaryDao) {
		this.searchConditionDictionaryDao = searchConditionDictionaryDao;
	}


	
}
